#ifndef BOX_INSTANCE_H_INCLUDED
#define BOX_INSTANCE_H_INCLUDED

#include <Core/Box/ITransportLayer.h>
#include <Core/Box/IBoxCommandIFace.h>
#include <Core/Box/IBoxEventSubscriber.h>

#include <list>
#include <string>
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>

namespace BoxCore {

/**
 * The BoxInstanceParams
 */
struct BoxInstanceParams {
	std::string name;		// Box instance name
	bool closeGraceful; 
};

/**
 * The BoxInstance
 */
class BoxInstance : boost::noncopyable {
public:
    explicit BoxInstance(BoxInstanceParams const & params);
    ~BoxInstance();
	
	bool initialize(ITransportLayerPtr transport, IBoxCommandIFacePtr commandIface);

	bool registrEventSubscriber(IBoxEventSubscriberPtr boxEvSubscriber);
	void unregistrEventSubscriber(IBoxEventSubscriberPtr boxEvSubscriber);
	
	inline BoxInstanceParams const & getParams() const 
		{ return m_params; }

    bool startInstance();
	void interruptInstance();
	void waitInstance();
	
	inline std::string getLastError() const 
	{ 
		boost::lock_guard<boost::mutex> guard(m_lock);
		return m_lastError; 
	}

private:
	inline bool setLastError(std::string const & message) 
		{ m_lastError = message; return false; }

	BoxInstanceParams const m_params;						// Init params
	boost::mutex mutable m_lock;							// Main locker
	std::list<IBoxEventSubscriberPtr> m_boxEvSubscribers;	// List of subscribers
	ITransportLayerPtr m_transport;							// Transport impl
	IBoxCommandIFacePtr m_commandIface;						// Command interface implementation
	std::string mutable m_lastError;

};

} // namespace Core

#endif 

